<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">企业名称：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary" @click="search">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/park/enterprise/Add')">添加企业</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <template>
        <el-table style="width: 100%" :data="buildingList" @expand-change="handleExpandChange">
          <!-- 嵌套表格 -->
          <el-table-column type="expand">
            <template #default>
              <el-table :data="RentList">
                <el-table-column label="租赁楼宇" width="320" prop="buildingName" />
                <el-table-column label="租赁起始时间" prop="startTime" />
                <el-table-column label="合同状态" prop="status">
                  <template #default="{ row }">
                    <span>{{ row.status === 0 ? '待生效' : row.status === 1 ? '生效中' : row.status === 2 ? '已到期' : row.status === 3 ? '已退租' : '错误状态' }}</span>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                  <template #default="{ row }">
                    <el-button size="mini" type="text" :disabled="row.status === 3" @click="backRent(row.id)">退租</el-button>
                    <el-button size="mini" type="text" :disabled="row.status !== 3">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </el-table-column>
          <el-table-column type="index" label="序号" />
          <el-table-column label="企业名称" width="320" prop="name" />
          <el-table-column label="联系人" prop="contact" />
          <el-table-column
            label="联系电话"
            prop="contactNumber"
          />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button size="mini" type="text" @click="addRent(row.id)">添加合同</el-button>
              <el-button size="mini" type="text" @click="$router.push(`/park/enterprise/components/look/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="$router.push(`/park/enterprise/Add/${row.id}`)">编辑</el-button>
              <el-popconfirm title="确定删除该企业吗？" @confirm="delEnterprise(row.id)">
                <el-button slot="reference" size="mini" type="text" style="margin-left: 10px;">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        :current-page="params.page"
        @current-change="handleCurrentChange"
      />
    </div>
    <createContract :value="id" :show-dialog.sync="showDialog" @update="getEnterprise" />
  </div>
</template>

<script>
import { getEnterprise, delEnterprise, getContract, delContract } from '@/api/Enterprise'
import createContract from './components/create-contract.vue'
export default {
  components: { createContract },
  data() {
    return {
      buildingList: [],
      params: {
        page: 1,
        pageSize: 10,
        name: ''
      },
      RentList: [],
      total: 0,
      showDialog: false,
      id: null
    }
  },
  created() {
    this.getEnterprise()
  },
  methods: {
    // 退租功能
    backRent(id) {
      this.$confirm('确定退租该企业吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delContract(id)
        this.$emit('update')
        this.$message.success('退租成功')
      }).catch(() => {
        this.$message.info('已取消退租')
      })
    },
    // 展开功能
    async handleExpandChange(row, expandedRows) {
      // console.log('展开或关闭', row, expandedRows)
      const isExpand = expandedRows.find(item => item.id === row.id)
      if (isExpand) {
        const res = await getContract(row.id)
        // console.log(res)
        this.RentList = res.data
      }
    },
    // 添加合同
    addRent(id) {
      this.showDialog = true
      this.$nextTick(() => {
        this.id = id
      })
    },
    // 删除功能
    async delEnterprise(id) {
      // console.log(id)
      await delEnterprise(id)
      this.$message.success('删除成功')
      this.getEnterprise()
    },
    // 模糊搜索功能
    search() {
      this.getEnterprise()
      this.params.page = 1
    },
    // 分页功能
    handleCurrentChange(newPage) {
      // 把点击的页数赋值给请求参数页数
      this.params.page = newPage
      // 使用最新的请求参数获取列表数据
      this.getEnterprise()
    },
    // 获取企业信息
    async getEnterprise() {
      const res = await getEnterprise(this.params)
      // console.log(res)
      this.buildingList = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
